package org.ligson.cg;

import org.apache.commons.lang.ArrayUtils;
import org.ligson.cg.folder.init.CreateApp;
import org.ligson.cg.orm.db.CreateEntity;

import java.io.File;
import java.util.Arrays;

public class SoEasy {
    private static String helpInfo = "用例(*代表可选):soeasy [选项]* [参数]*\n" +
            "选项:\n" +
            "\tcreate-app 创建一个应用\n" +
            "\tcreate-entity 创建实体代码\n" +
            "\thelp <选项>";

    public static void main(String[] args) throws Exception {
        System.out.println(Arrays.toString(args));
        if (ArrayUtils.isEmpty(args)) {
            System.out.println(helpInfo);
            return;
        }
        String optName = args[0];
        if (optName.equals("help")) {
            if (args.length > 1) {
                String subOptName = args[1];
                if (subOptName.equals("create-app")) {
                    System.out.println("create-app [应用名称] [基础包名] [路径]*");
                } else if (subOptName.equals("create-entity")) {
                    System.out.println("create-entity -t [类型] [orm配置文件路径]");
                    System.out.println("              -t 可选,默认:所有(all),mapper:仅生成mapper文件,dao,service,controller");
                }
            } else {
                System.out.println(helpInfo);
                return;
            }
        } else if (optName.equals("create-app")) {
            if (args.length >= 3) {
                String appName = args[1];
                String packageName = args[2];
                File destFile;
                if (args.length == 4) {
                    destFile = new File(args[3]);
                } else {
                    destFile = new File(".");
                }
                CreateApp createAppProcess = new CreateApp();
                createAppProcess.createApp(appName, packageName, destFile.getPath());
            } else {
                System.out.println("create-app [应用名称] [基础包名] [路径]*");
            }
        } else if (optName.equals("create-entity")) {
            if (args.length > 1) {
                String type = "all";
                String ormFile = args[1];
                if (args.length == 4) {
                    type = args[2];
                    ormFile = args[3];
                }
                CreateEntity.createEntity(ormFile, type);
            } else {
                System.out.println("create-entity [orm配置文件路径]");
            }
        }
    }
}
